 //控制层 
app.controller('specificationController' ,function($scope,$controller,specificationService,specificationOptionService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		specificationService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		specificationService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		specificationService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	/*//保存
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=specificationService.update( $scope.entity ); //修改  
		}else{
			serviceObject=specificationService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.reloadList();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}*/
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		specificationService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.reloadList();//刷新列表
					$scope.selectIds=[];
				}						
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		specificationService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}

	//添加修改对象
	$scope.entity={
		specification: {},
		specOptions: []
	};


	//添加选项
	$scope.addOption = function () {

		$scope.entity.specOptions.push({});

	}

	//根据下标移除选项
	$scope.removeOption = function (index) {

		$scope.entity.specOptions.splice(index,1);

	}

	//添加或修改
	$scope.save = function () {

		var obj = null;

		//根据id判断是修改还是添加
		if ($scope.entity.specification.id == null ) {
			obj = specificationService.add($scope.entity);
		}else {
			obj = specificationService.update($scope.entity);
		}

		obj.success(
			function(response){

				console.log(response.success);
				console.log(response);

				if(response.success){
					$scope.reloadList();//刷新列表
				}

				//清空对象
				$scope.entity={
					specification: {},
					specOptions: []
				};
			}
		);

	}

	//获取当前需要修改的对象
	$scope.getOne = function (index) {

		$scope.entity.specification = $scope.list[index];

		//查询绑定的规格选项
		specificationOptionService.findBySpecId($scope.entity.specification.id).success(function (res) {

			$scope.entity.specOptions=res;

		});

	}
    
});	
